<?php
if (!ob_start("ob_gzhandler")) {
  ob_start();
}

require_once dirname(__FILE__) . '/common.inc.php';
if (isset($_COOKIE[CLIENT])) {
  $_SESSION[CLIENT] = json_decode($_COOKIE[CLIENT]);
}

$opt = new stdClass();
$opt->select_expr = 't.*, t1.folder AS folder';
$opt->join_expr = 'LEFT JOIN pz_theme AS t1 ON t1.id = t.theme_id';
$tmp = DbUtils::get(DbUtils::createPdoInst(), 'pz_profile', NULL, NULL, NULL, NULL, NULL, $opt)->d;
$profile = new stdClass();
if (count($tmp) > 0) {
  $profile = $tmp[0];
}

if (!isset($profile->folder)) {
  die('No Theme Selected');
}

$logout = MiscUtils::getParam('logout', NULL);
if ($logout === '') {
  unset($_SESSION[CLIENT]);
  setcookie(CLIENT, '', time() - 1, PATH, DOMAIN);
}

$mRoot = new stdClass();
$mRoot->id = 0;
$mRoot->name = 'ROOT';
$mRoot->link = '';
$mRoot->c = array();
$cond_vals = new stdClass();
$cond_vals->c = 't.appear_in_menu = :v1';
$cond_vals->v = array(':v1' => 1);
_createMenuRoot(DbUtils::get(DbUtils::createPdoInst(), 'pz_menu', $cond_vals, 't.order', 'ASC', NULL, NULL, NULL)->d, $mRoot);

$id = MiscUtils::getParam('i', NULL);
if (!$id) {
  $cond_vals = new stdClass();
  $cond_vals->c = 't.is_default = :v1';
  $cond_vals->v = array(':v1' => 1);
  $tmp = DbUtils::get(DbUtils::createPdoInst(), 'pz_menu', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
  if (count($tmp) == 1) {
    $id = $tmp[0]->id;
  }
}

if ($id) {
  $cond_vals = new stdClass();
  $cond_vals->c = 't.id = :v1';
  $cond_vals->v = array(':v1' => $id);
  $tmp = DbUtils::get(DbUtils::createPdoInst(), 'pz_menu', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
  $menu = new stdClass();
  $menu->name = 'CAN NOT FOUND THE PAGE';
  if (count($tmp) > 0) {
    $menu = $tmp[0];
  }
  
  
  $cond_vals = new stdClass();
  $cond_vals->c = 't.menu_id = :v1';
  $cond_vals->v = array(':v1' => $id);
  $tmp = DbUtils::get(DbUtils::createPdoInst(), 'pz_page', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
  $page = new stdClass();
  if (count($tmp) > 0) {
    $page = $tmp[0];
  } else {
    $page->html = '';
    $page->js = '';
    $page->css = '';
  }
  if ($profile->analytics_code != '') {
    $page->js = "
        var _gaq = _gaq || [];
        _gaq.push(['_setAccount', '$profile->analytics_code']);
        _gaq.push(['_trackPageview']);
      
        (function() {
          var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
          ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
          var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
        })();
    " . $page->js;
  }
  $cond_vals = new stdClass();
  $cond_vals->c = 't.menu_id = :v1';
  $cond_vals->v = array(':v1' => $id);
  $opt = new stdClass();
  $opt->select_expr = 't.*, t1.name, t1.folder';
  $opt->join_expr = 'LEFT JOIN pz_plugin AS t1 ON t1.id = t.plugin_id';
  $tmp = DbUtils::get(DbUtils::createPdoInst(), 'pz_plugin_instance', $cond_vals, NULL, NULL, NULL, NULL, $opt)->d;
  foreach ($tmp as &$instance) {
    $script = _script($instance->folder, $instance->id, $instance->width, $instance->height, $instance->scrolling);
    $page->html = str_replace("{" . $instance->folder . ": " . "$instance->id" . "}", $script, $page->html);
  }
  
  $mRoot2 = new stdClass();
  $mRoot2->id = 0;
  $mRoot2->name = 'ROOT';
  $mRoot2->link = '';
  $mRoot2->c = array();
  _createMenuRoot(DbUtils::get(DbUtils::createPdoInst(), 'pz_menu', NULL, 't.order', 'ASC', NULL, NULL, NULL)->d, $mRoot2);
  $nav = _getNav($mRoot2, $id);
  if (count($nav) >= 1) {
    $id = $nav[0]->id;
    if (count($nav) == 1) {
//      $nav = array();
    }
  }
} else {
  $menu = new stdClass();
  $menu->name = 'No Page Available';
  
  $page = new stdClass();
  $page->html = '';
  $page->js = '';
  $page->css = '';
  
  $nav = array();
}

include dirname(__FILE__) . "/ext/ui/bin/$profile->folder/base.php";
return;

function _createMenuRoot($arr, $node) {
  for ($i = 0, $il = count($arr); $i < $il; $i++) {
    $tmp = $arr[$i];
    if ($tmp->parent_id == $node->id) {
      $tmp->link = '?i=' . $tmp->id;
      $tmp->c = array();
      $node->c[] = $tmp;
      _createMenuRoot($arr, $tmp);
    }
  }
}

function _getNav($root, $id) {
  $array = array();
  return _find($root, $id, $array);
}

function _find($haystack, $needle, $array) {
  if ($haystack->id == $needle) {
    return $array;
  }
  foreach ($haystack->c as &$tmp) {
    array_push($array, $tmp);
    $result = _find($tmp, $needle, $array);
    if ($result != NULL) {
      return $result;
    }
    array_pop($array);
  }
  return array();
}

function _script($folder, $instance_id, $width, $height, $scrolling) {
  return "<div style=\"padding: 0;margin: 0;border: 0;width: $width; height: $height;\">
            <script type=\"text/javascript\">
              function updateHeight() {
                var ifm_$instance_id = document.getElementById(\"plgn_ifm_$instance_id\");
                if (ifm_$instance_id && !window.opera){
                  if (ifm_$instance_id.contentDocument && ifm_$instance_id.contentDocument.body){
                    var boh = ifm_$instance_id.contentDocument.body.offsetHeight;
                    var bsh = ifm_$instance_id.contentDocument.body.scrollHeight;
                    ifm_$instance_id.height = Math.max(Math.max(boh, bsh), $height);
                  }
                }
                YAHOO.util.CrossFrame.send('" . PARENT_URL . "', 'parent', document.body.offsetHeight);
                parent.window.postMessage(document.body.offsetHeight, '" . PARENT_DOMAIN . "');
              }
            </script>
            <iframe id=\"plgn_ifm_$instance_id\" onload=\"Javascript:updateHeight()\" width=\"$width\" height=\"$height\"" . (($scrolling == 0) ? " scrolling=\"no\"" : "") . " frameborder=\"0\"  marginwidth=\"0\" marginheight=\"0\" allowtransparency=\"true\" src=\"instance.php?inst_i=$instance_id&inst_f=$folder&inst_s=index&" . $_SERVER['QUERY_STRING'] . "\"></iframe>
          </div>";
}
?>